package com.wangyb.multitenant.controller;

import java.util.List;
import java.util.Arrays;

import com.wangyb.multitenant.service.IMtTenantService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.wangyb.multitenant.vo.MtTenantVo;
import com.wangyb.multitenant.bo.MtTenantQueryBo;
import com.wangyb.multitenant.bo.MtTenantAddBo;
import com.wangyb.multitenant.bo.MtTenantEditBo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 租户Controller
 *
 * @author ruoyi
 * @date 2021-06-23
 */
@Api(value = "租户控制器", tags = {"租户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/multitenant/tenant")
public class MtTenantController extends BaseController {

    private final IMtTenantService iMtTenantService;

    /**
     * 查询租户列表
     */
    @ApiOperation("查询租户列表")
    @PreAuthorize("@ss.hasPermi('multitenant:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo<MtTenantVo> list(@Validated MtTenantQueryBo bo) {
        return iMtTenantService.queryPageList(bo);
    }

    /**
     * 导出租户列表
     */
    @ApiOperation("导出租户列表")
    @PreAuthorize("@ss.hasPermi('multitenant:tenant:export')")
    @Log(title = "租户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MtTenantVo> export(@Validated MtTenantQueryBo bo) {
        List<MtTenantVo> list = iMtTenantService.queryList(bo);
        ExcelUtil<MtTenantVo> util = new ExcelUtil<MtTenantVo>(MtTenantVo.class);
        return util.exportExcel(list, "租户");
    }

    /**
     * 获取租户详细信息
     */
    @ApiOperation("获取租户详细信息")
    @PreAuthorize("@ss.hasPermi('multitenant:tenant:query')")
    @GetMapping("/{tenantId}")
    public AjaxResult<MtTenantVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("tenantId") Long tenantId) {
        return AjaxResult.success(iMtTenantService.queryById(tenantId));
    }

    /**
     * 新增租户
     */
    @ApiOperation("新增租户")
    @PreAuthorize("@ss.hasPermi('multitenant:tenant:add')")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MtTenantAddBo bo) {
        return toAjax(iMtTenantService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改租户
     */
    @ApiOperation("修改租户")
    @PreAuthorize("@ss.hasPermi('multitenant:tenant:edit')")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MtTenantEditBo bo) {
        return toAjax(iMtTenantService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除租户
     */
    @ApiOperation("删除租户")
    @PreAuthorize("@ss.hasPermi('multitenant:tenant:remove')")
    @Log(title = "租户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{tenantIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] tenantIds) {
        return toAjax(iMtTenantService.deleteWithValidByIds(Arrays.asList(tenantIds), true) ? 1 : 0);
    }
}
